package com.yczy365.petmall.controller;

import com.yczy365.petmall.mapper.AdminMapper;
import com.yczy365.petmall.pojo.dto.AdminDTO;
import com.yczy365.petmall.pojo.vo.AdminVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController

public class AdminController {
    @Autowired(required = false)
    private AdminMapper mapper;
    @RequestMapping("/admin")
    public int admin(@RequestBody AdminDTO adminDTO, HttpSession session, HttpServletResponse response){
        System.out.println(adminDTO);
        AdminVO adminVO = mapper.selectByUsername(adminDTO.getUsername());
        if (adminVO!=null){
            if (adminVO.getPassword().equals(adminDTO.getPassword())){
                session.setAttribute("admin",adminVO);
                if (adminDTO.getRem()){
                    Cookie c1 = new Cookie("username", adminVO.getUsername());
                    Cookie c2 = new Cookie("password", adminVO.getPassword());
                    c1.setMaxAge(60*60*24*30);
                    response.addCookie(c1);
                    response.addCookie(c2);
                    session.setAttribute("admin",adminVO);
                }
                return 1;
            }return 2;

        }return 3;
    }
    @RequestMapping("/adminCurrentAdmin")
    public AdminVO adminCurrentAdmin(HttpSession session){
        AdminVO admin = (AdminVO) session.getAttribute("admin");
        return admin;
    }
}
